﻿using _1._6._2015_Movie.Data;
using _1._6._2015_Movie.Data.Model;
using _1._6._2015_Movie.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _1._6._2015_Movie.Adapters
{
    public class MovieDataAdapter : IMovieAdapter
    {
        public void CreateMovie(MovieViewModel movieView)
        {
            using(MovieTheaterDbContext db = new MovieTheaterDbContext())
            {
                db.Movies.Add(new Movie{ 
                   Description = movieView.Description, 
                   PosterUrl = movieView.PosterUrl,
                   Title = movieView.Title,
                   Rating = movieView.Rating
                });
            }

        }

        public List<MovieViewModel> GetMovies() // if wanted to return a single movie in list coud pass optional param as so GetMoview(int id = -1) and test if < 0 we want one
        {

            List<MovieViewModel> movies = null;
            using (MovieTheaterDbContext db = new MovieTheaterDbContext())
            {
                movies = db.Movies.Select(m => new MovieViewModel
                {
                    Description = m.Description,
                    PosterUrl = m.PosterUrl,
                    Rating = m.Rating,
                    Title = m.Title,
                    MovieID = m.MovieId
                }).ToList();
            }
            return movies;
        }

        public MovieViewModel GetMovie(int id)
        {
            MovieViewModel movieView = new MovieViewModel();
            Movie movie = null;
            using (MovieTheaterDbContext db = new MovieTheaterDbContext())
            {
                movie = db.Movies.FirstOrDefault(m => m.MovieId == id);
            }
            if (movie != null)
            {
                movieView.Description = movie.Description;
                movieView.MovieID = movie.MovieId;
                movieView.PosterUrl = movie.PosterUrl;
                movieView.Rating = movie.Rating;
                movieView.Title = movie.Title;
            }
            return movieView;
        }
        public void UpdateMovie(MovieViewModel model)
        {
            Movie movie = null;
            using (MovieTheaterDbContext db = new MovieTheaterDbContext())
            {
                movie = db.Movies.FirstOrDefault(m => m.MovieId == model.MovieID);
                if (movie != null)
                {
                    movie.Description = model.Description;
                    movie.MovieId = model.MovieID;
                    movie.PosterUrl = model.PosterUrl;
                    movie.Rating = model.Rating;
                    movie.Title = model.Title;

                    db.SaveChanges();
                }
            }
        }
        public void DeleteMovie(int id)
        {
            Movie movie = null;
            using (MovieTheaterDbContext db = new MovieTheaterDbContext())
            {
                movie = db.Movies.FirstOrDefault(m => m.MovieId == id);
                db.Movies.Remove(movie);

                db.SaveChanges();
            }
        }


    }
}




/*        -------------------------------------------------            
namespace _1._6._2015_Movie.Controllers
{
    public class MovieController : Controller
    {
  
        public ActionResult Detail(MovieViewModel model)
        {

            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

*/